package com.fw.ui.view.settings.usercontrol.parentmerchant.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.merchantprofile.bean.MerchantProfileBean;
import com.fw.service.merchantprofile.bean.SearchMerchantProfileBean;
import com.fw.ui.bao.MerchantProfileBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.CreateParentMerchantView;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class CreateParentMerchantClearButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 482478998438351054L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private MerchantProfileBAO merchantProfileBAO;
    @Autowired
    private UICommonUtils uiCommonUtils;

    @SuppressWarnings("rawtypes")
    @Override
    public Map<String, Object> click(ClickEvent event) {
	try {
	    CreateParentMerchantView createParentMerchantView = (CreateParentMerchantView) this.getBasedComponent();

	    uiCommonUtils.clearValueAllUIComponentExcludeLabel(createParentMerchantView);
	    uiCommonUtils.setVisibleAllUIComponentWithIncludePrefix(createParentMerchantView, false);

	    String sortingDefault = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.settings.usercontrol.parentmerchant.CreateParentMerchantView.default.table.sorting");

	    SearchCriteriaBean<SearchMerchantProfileBean> searchCriteriaBean = createParentMerchantView.getSearchCriteriaBean();
	    SearchMerchantProfileBean searchMerchantProfileBean = new SearchMerchantProfileBean();
	    searchMerchantProfileBean.setSearchAllStatus(Boolean.TRUE);
	    searchCriteriaBean.setDirection1(SearchCriteriaBean.DIRECTION_ASC);
	    searchCriteriaBean.setOrder1(sortingDefault);
	    searchCriteriaBean.setCriteriaBean(searchMerchantProfileBean);
	    searchCriteriaBean.setPage(1);
	    searchCriteriaBean.setSize(30);
	    createParentMerchantView.getSelectedBeanItemMap().clear();
	    
	    SearchResultBean<MerchantProfileBean> searchResultBean = merchantProfileBAO.selectMerchantProfile(searchCriteriaBean);

	    Integer totalPage = searchResultBean.getTotalPages();
	    if (totalPage == null) {
		totalPage = 1;
	    }
	    Long totalElement = searchResultBean.getTotalElements();
	    if (totalElement == null) {
		totalElement = 0l;
	    }
	    List<BeanItem> beanItems = createParentMerchantView.generateTableItemBean(searchResultBean);

	    createParentMerchantView.getTableMerchantPanel().clearTableData();
	    createParentMerchantView.getTableMerchantPanel().setManualPageTable(1);
	    createParentMerchantView.getTableMerchantPanel().setTableBeanContainerDataTotalPage(beanItems, totalPage, totalElement);

	    createParentMerchantView.getTableMerchantSelectedPanel().clearTableData();
	    createParentMerchantView.getTableMerchantSelectedPanel().setManualPageTable(1);
	    createParentMerchantView.getTableMerchantSelectedPanel().setTableBeanContainerDataTotalPage(new ArrayList<BeanItem>(), 0, 0l);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;
    }

    @Override
    public boolean validate(ClickEvent event) {
	return true;
    }

}
